package logic;

import java.sql.SQLException;

import ui.MessagePopUp;

/** Represents a withdrawal ATM transaction
 * @author Matthias
 * 
 */
public class Withdrawal extends Transaction {
	 
	/** Invokes the overridden superclass's constructor perform
	 * @param perform is the PerformTransaction object
	 */
	public Withdrawal(final PerformTransactions perform) {
		super(perform);
	}
 
	/**
	 * @see logic.Transaction#execute()
	 */
	@Override
	public void execute() {
		// cash was not dispensed yet
		boolean cashDispensed = false; 
		
		// declaring the local variable to be 0 from start
		double withdrawalCustomAmount = 0;
		// setting the variable to have the input of the dialog box
		withdrawalCustomAmount = getClassPerform().getClassATM().getClassScreen().
				getAccWithdrawal().getWithdrawInputOfDialog();
		
		// declaring the local variable to be 0 from start
		double withdrawalRadioBtnAmount = 0;
		//setting the variable to have the input the selected radiobutton
		withdrawalRadioBtnAmount = getClassPerform().getClassATM().getClassScreen().
				getAccWithdrawal().getWithdrawRdbtnAmount();
		
		// summing both previous variables together, in order to compare it with the
		// totalbalance.
		double withdrawalSum = withdrawalRadioBtnAmount + withdrawalCustomAmount;

		// check whether the user has enough money in the account 
		if (withdrawalSum <= getTotBal()) { 
				try {
					getClassPerform().getClassATM().getClassBdbAccSQL().debitAccSQL(getAccNr(), withdrawalSum);	
					cashDispensed = true; // cash was dispensed
				} catch (SQLException e) {
					String text = "Withdrawal incomplete! Please try again";
					new MessagePopUp().setError(text);	
				}
		} // end if
		else { // cash dispenser does not have enough cash
			String text = "Insufficient cash available in the ATM." +
					"\n\nPlease choose a smaller amount.";
			new MessagePopUp().setError(text);
		} // end else
	} // end method

} // end class Withdrawal